package cn.xxstudy.permission.help.view

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.xxstudy.permission.R
import cn.xxstudy.permission.help.getPermissionMap

/**
 * @author ：wuxinxi on 2020/11/11 .
 * @packages ：cn.xxstudy.permission.help.view .
 * TODO:Dialog
 */
class PermissionDialog {
    companion object {
        fun show(
            context: Context,
            permissions: Array<String>,
            title: String,
            content: String,
            positive: () -> Unit,
            negative: () -> Unit
        ) {
            val recyclerView = RecyclerView(context)
            val spanCount = 3
            recyclerView.layoutManager = GridLayoutManager(context, spanCount)
            recyclerView.addItemDecoration(GridItemDecoration(spanCount))
            recyclerView.overScrollMode = RecyclerView.OVER_SCROLL_NEVER
            recyclerView.adapter = PermissionAdapter(permissionNameToResId(context, permissions))
            AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(content)
                .setView(recyclerView)
                .setPositiveButton("确定") { dialog, _ ->
                    positive()
                    dialog.cancel()
                }
                .setNegativeButton("取消") { dialog, _ ->
                    negative()
                    dialog.cancel()
                }.show()

        }

        private fun permissionNameToResId(context: Context, permissions: Array<String>): Array<Int> {
            val list = HashSet<Int>()
            for (permission in permissions) {
                val permissionGroup = when (Build.VERSION.SDK_INT) {
                    Build.VERSION_CODES.Q -> getPermissionMap()[permission]
                    else -> {
                        val permissionInfo = context.packageManager.getPermissionInfo(permission, 0)
                        permissionInfo.group
                    }
                }

                permissionGroup?.let {
                    list.add(context.packageManager.getPermissionGroupInfo(it, 0).icon)
                }
            }

            return list.toTypedArray()
        }
    }

}

class PermissionAdapter(private val resList: Array<Int>) : RecyclerView.Adapter<PermissionHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PermissionHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.permission_item, parent, false)
        return PermissionHolder(view)
    }

    override fun getItemCount(): Int = resList.size

    override fun onBindViewHolder(holder: PermissionHolder, position: Int) {
        holder.img.setColorFilter(Color.GRAY)
        holder.img.setImageResource(resList[position])
    }

}

class PermissionHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val img: ImageView = itemView.findViewById(R.id.img)
}